<?php

namespace App\Admin\Controllers;

use App\Models\Movie;
use App\Http\Controllers\Controller;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Layout\Row;

class ChartjsController extends Controller
{
    public function index(Content $content)
    {
        // 评分的数据
        // 0分 <3  <5 <7 <8 <9
        $data[0] = Movie::where('score', 0)->count();
        $data[1] = Movie::where('score', '>', 0)->where('score', '<=', 3)->count();
        $data[2] = Movie::where('score', '>', 3)->where('score', '<=', 5)->count();
        $data[3] = Movie::where('score', '>', 5)->where('score', '<=', 7)->count();
        $data[4] = Movie::where('score', '>', 7)->where('score', '<=', 8)->count();
        $data[5] = Movie::where('score', '>', 8)->where('score', '<=', 9)->count();
        $data[6] = Movie::where('score', '>', 9)->count();
        $moviePie = [
            'datasets' => [
                [
                    'data' => $data,
                    'backgroundColor' => [
                        "rgb(255, 99, 132)",
                        "rgb(54, 162, 235)",
                        "rgb(255, 205, 86)",
                        "rgb(123, 104, 238)",
                        "rgb(30, 144, 255)",
                        "rgb(255, 165, 0)",
                        "rgb(255, 127, 80)",
                        "rgb(255, 99, 71)"
                    ],
                ],
            ],
            'labels' => [
                '0分',
                '低于3分',
                '低于5分',
                '低于7分',
                '低于8分',
                '低于9分',
                '高于9分',
            ]
        ];
        $moviePeople = $this->scorePeople();
        return $content
            ->header('电影数据统计')
            ->row(function(Row $row) use ($moviePie, $moviePeople) {
                $type = 'chartjs1';
                $row->column(3, new Box('按评分统计', view('admin.chartjs', compact('moviePie', 'type'))));
                $row->column(3, new Box('按评分人数统计', view('admin.chartjs', ['moviePie' => $moviePeople, 'type' => 'people'])));
            });
    }

    public function scorePeople()
    {
        // 评分人数的数据
        // 0分 <3  <5 <7 <8 <9
        $data[0] = Movie::where('score_people', 0)->count();
        $data[1] = Movie::where('score_people', '>', 0)->where('score', '<=', 10000)->count();
        $data[2] = Movie::where('score_people', '>', 10000)->where('score', '<=', 100000)->count();
        $data[3] = Movie::where('score_people', '>', 100000)->where('score', '<=', 300000)->count();
        $data[4] = Movie::where('score_people', '>', 300000)->where('score', '<=', 500000)->count();
        $data[5] = Movie::where('score_people', '>', 500000)->where('score', '<=', 1000000)->count();
        $data[6] = Movie::where('score_people', '>', 1000000)->count();
        return $moviePie = [
            'datasets' => [
                [
                    'data' => $data,
                    'backgroundColor' => [
                        "rgb(255, 99, 132)",
                        "rgb(54, 162, 235)",
                        "rgb(255, 205, 86)",
                        "rgb(123, 104, 238)",
                        "rgb(30, 144, 255)",
                        "rgb(255, 165, 0)",
                        "rgb(255, 127, 80)",
                        "rgb(255, 99, 71)"
                    ],
                ],
            ],
            'labels' => [
                '没有人评分',
                '少于1万人',
                '超过1万人',
                '超过10万人',
                '超过30万人',
                '超过50万人',
                '超过100万人',
            ]
        ];

    }
}
